package com.lanhai.fnstore;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.lanhai.fnstore.cache.AgvItem;
import com.lanhai.fnstore.cache.AgvNextTransEnum;
import com.lanhai.fnstore.cache.ParkCache;
import com.lanhai.fnstore.cache.ParkItem;
import com.lanhai.fnstore.cache.ProcessCache;
import com.lanhai.fnstore.cache.ProcessSelectTypeEnum;
import com.lanhai.fnstore.cache.StationGroupCache;
import com.lanhai.fnstore.localstore.AgvLocalBean;
import com.lanhai.fnstore.localstore.AgvTools;
import com.lanhai.fnstore.log.LogData;

public class Main {
	 private static final Logger logger = Logger.getLogger(Main.class);
	 
	public static void main44(String[] args) throws Exception {
		logger.info("程序启动....");  
		Config cnf=new Config();
		logger.info("加载配置,路径 D:\\fnstore\\fnstore.conf...."); 
		try {
			cnf.loadConf();
		}catch(Exception e) { 
			logger.error( "加载配置出错，程序将自动退出",e); 
			System.exit(0); 
		}
		
		logger.info("初始化数据中..."); 
		try {
			LoadDB.loadDbData(cnf);
		}catch(Exception e) {
			logger.error( "加载数据出错，程序将自动退出",e);  
			System.exit(0); 
		}
		logger.info("初始化结束，准备启动侦听..."); 
		DispatchCore core=new DispatchCore(cnf);
		core.Start();
		 
		logger.info( "程序启动成功，输入大写C退出");  
		while(System.in.read()!=67) {
			
		}
		core.Stop();
		logger.info( "程序正常结束");   
		System.exit(0);
	}
	
	public static void mainxxx (String[] args) throws Exception {
		Config cnf=new Config();
		logger.info("加载配置,路径 D:\\fnstore\\fnstore.conf...."); 
		try {
			cnf.loadConf();
		}catch(Exception e) { 
			logger.error( "加载配置出错，程序将自动退出",e); 
			System.exit(0); 
		}
		
		logger.info("加载数据中..."); 
		try {
			LoadDB.loadDbData(cnf);
		}catch(Exception e) {
			logger.error( "加载数据出错，程序将自动退出",e);  
			System.exit(0); 
		}
		logger.info("wwwwwwwwwww"); 
		//设置部分机台不可用
	
		String nextProcess="测试上料";
		String nextProcess2="测试上料2组";
		/*
		int stationgroupid=123;
		StationGroupCache.updateDisable(stationgroupid, true); 
		*/
		
		//锁机台
		StationGroupCache.lockGroup("L7A", 111); 
		StationGroupCache.updateDisable(75, true); 
		
		//拿机台
		ProcessSelectTypeEnum selectType=ProcessCache.getTypeByProcess(nextProcess); 
		List<Integer> pitList=null;
		switch(selectType) {
		case WeightAndNum:
			pitList=StationGroupCache.getCanUseParkNoListByWeightAndNum(nextProcess,nextProcess2,null);
		 
			break;
		case Num:
			pitList=StationGroupCache.getCanUseParkNoListByNum(nextProcess,null);
			break;
		case Weight:
			pitList=StationGroupCache.getCanUseParkNoListByWeight(nextProcess,null);
			break; 
		} 
		System.out.println("打印拿到的可用点位");
		for(Integer it:pitList) {
			System.out.println(it);
		}
		  
		ParkItem toTrans = ParkCache.getCanTransPark(pitList, selectType);
		if(toTrans==null) {
			System.out.println("没有可用的分配的点");
		}else {
			System.out.println("分配的点="+toTrans.getParkNo());
		}
	}
	
	public static void main_log  (String[] args) throws Exception {
		
		List<LogData> list=new ArrayList<LogData>();
		byte[] buf=new byte[4];
		long t=System.currentTimeMillis();
		for(int i=1;i<=100;i++) {
			buf[1]=(byte)i;
			
			LogData obj=new LogData();
			obj.setAgvId(i);
			obj.setIp("127.1.1.2"); 
			obj.setCmdtype(0);
			obj.setReceiveData( Tools.byte2HexStr(buf));
			obj.setReceiveRemark( "测试");
			obj.setCreateTime(t+i);
			obj.setSendData("1" );
			obj.setSendRemark("2");
			list.add(obj);
		}
		
		
		  Gson gson=new Gson();
		  LogTemSp lt=new LogTemSp();
		  lt.setList(list);
		 String  json=gson.toJson(lt);
		 String logurl="http://127.0.0.1:5000/api/Cmdlog/add";
		 Tools.post(logurl, json); 
		 logger.info("程序正常结束");  
		System.exit(0);
	}
	  

	public static void maingson   (String[] args) throws Exception {
		 AgvItem item=new AgvItem();
		 item.setAgvId(1);
		 item.setAreaId(1);
		 item.setCurrentX(1);
		 item.setNextTrans(AgvNextTransEnum.UpInAndDownOut);
		  
		 Gson gson=new Gson();
		 String s=gson.toJson(item.toBean());
		
		 System.out.println(s); 
		
		 
		 AgvLocalBean bean=gson.fromJson(s, AgvLocalBean.class);
		 System.exit(0);
	}
}

class LogTemSp{
	private List<LogData> list;

	public List<LogData> getList() {
		return list;
	}

	public void setList(List<LogData> list) {
		this.list = list;
	}
	
	
}